/**
 * @description 格式化时间
 * @author zzg
 * @date 2019-01-15
 * @export
 * @param {*} time
 * @param {*} cFormat '{y}-{m}-{d} {h}:{i}:{s}' '{a}'星期几
 * @returns
 */
function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if (('' + time).length === 10) time = parseInt(time) * 1000
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value]
    }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}
/**
 *
 * @description 获取当前时间 包含 年月日 时分秒
 * @param {*} seperator1 年月日分隔符
 * @param {*} seperator2 时分秒分隔符
 * @returns
 */
function getTime(seperator1 = '-', seperator2 = ':') {
  var date = new Date()
  var month =
    date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  var strDate = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  var hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  var minutes =
    date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  var second =
    date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  var currentdate =
    date.getFullYear() +
    seperator1 +
    month +
    seperator1 +
    strDate +
    ' ' +
    hour +
    seperator2 +
    minutes +
    seperator2 +
    second
  return currentdate
}
/**
 *
 * @description 获取当天是星期几
 * @returns
 */
function getWeek() {
  let arr = ['日', '一', '二', '三', '四', '五', '六']
  let week = new Date().getDay()
  return arr[week]
}

export { parseTime, getTime, getWeek }
